﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace Refl
{
    public static class Paths
    {
        /// <summary>
        /// Gets and returns the installation path of the current executing version of the .Net Framework.
        /// </summary>
        /// <returns></returns>
        public static string GetFrameworkPath()
        {
            // This is the location of the .Net Framework Registry Key
            string framworkRegPath = @"Software\Microsoft\.NetFramework";

            // Get a non-writable key from the registry
            RegistryKey netFramework = Registry.LocalMachine.OpenSubKey(framworkRegPath, false);

            // Retrieve the install root path for the framework
            string installRoot = netFramework.GetValue("InstallRoot").ToString();

            // Retrieve the version of the framework executing this program
            string version = string.Format(@"v{0}.{1}.{2}\",
              Environment.Version.Major,
              Environment.Version.Minor,
              Environment.Version.Build);

            // Return the path of the framework
            return System.IO.Path.Combine(installRoot, version);
        }

        /// <summary>
        /// Gets and returns the installation path of the Windows SDK Tools
        /// </summary>
        public static string GetToolsPath()
        {
            // This is the location of the SDK Tools Registry Key
            string toolsRegPath = @"Software\Microsoft\Microsoft SDKs\Windows\v7.0A\WinSDK-NetFx40Tools-x86";

            // Get a non-writable key from the registry
            RegistryKey sdkTools = Registry.LocalMachine.OpenSubKey(toolsRegPath, false);

            // Retrieve the install root path for the framework
            string installRoot = sdkTools.GetValue("InstallationFolder").ToString();

            return installRoot;
        }
        /// <summary>
        /// Returns the installation path of the Windows SDK
        /// </summary>
        /// <returns></returns>
        public static string GetWindowsSDKPath()
        {
            return (new DirectoryInfo(GetToolsPath())).Parent.Parent.FullName;
        }
        public static string GetTempPath()
        {
            //should retrieve environment variable %temp%
            return Path.GetTempPath();
        }
        public static string GetVisualStudioInstallationPath()
        {
            string registryKeyString = @"SOFTWARE\Microsoft\VisualStudio\10.0\";

            using (RegistryKey localMachineKey = Registry.LocalMachine.OpenSubKey(registryKeyString))
            {
                return (new DirectoryInfo(localMachineKey.GetValue("InstallDir") as string)).Parent.Parent.FullName;
            }
        }
    }
}
